package base.demo;

import base.utils.PrintUtils;

import java.util.Arrays;

public class SelectSortDemo {


    static int[] arrays = {2, 3, 1, 5, 4};

    public static void main(String[] args) {
        selectSortDemo2(arrays);
    }

    public static void selectSort(int[] array) {

        int len = array.length;
        int[] newArray = Arrays.copyOf(array, len);
        int[] sub = new int[len];

        for (int i = 1; i < len; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = i - 1; j < len; j++) {

//                System.out.print(j+"  ,");
                int j1 = newArray[j];
//                int j2=newArray[j+1];
                min = Math.min(min, j1);
                System.out.println(min);
            }

            System.out.println();
//            System.out.println(min);
//            sub[i-1]=min;
        }

        System.out.println();

//        for (int item:sub){
//            System.out.print(item  +"  ");
//        }
    }


    public static void selectSort2(int[] array) {

        int len = array.length;
        int[] list = Arrays.copyOf(array, len);

        for (int i = 0; i < len; i++) {
            int sub = i;
            int min = list[i];
            for (int j = i + 1; j < len; j++) {
                int itemJ = list[j];
                if (min > itemJ) {
                    min = itemJ;
                    sub = j;
                }
            }
            if (i != sub) {
                int tmp = list[i];
                list[i] = list[sub];
                list[sub] = tmp;
            }
        }

        for (int item : list) {
            System.out.println(item);
        }


    }


    public static void bubleSortTestDemo(int[] array) {
        int len = array.length;
        int[] list = Arrays.copyOf(array, len);

        for (int i = 1; i < len; i++) {
            for (int j = 0; j < len - i; j++) {
                if (list[j] > list[j + 1]) {
                    int sub = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = sub;
                }
            }
        }
        for (int item : list) {
            System.out.print(item + "  ");
        }
    }

    public static void bubleSortTestDemo02(int[] array) {
        int len = array.length;
        int[] list = Arrays.copyOf(array, len);
        for (int i = 0; i < len - 1; i++) {
            int min = i;
            for (int j = i + 1; j < len; j++) {
                if (list[j] < list[min]) {
                    min = j;
                }
            }
            if (i != min) {
                int tmp = list[i];
                list[i] = list[min];
                list[min] = tmp;
            }
        }
    }

    public static void bubleSortTestDemo03(int[] array) {
        int len = array.length;
        int[] arr = Arrays.copyOf(array, len);

        for (int i = 0; i < len; i++) {
            int min = i;
            for (int j = i + 1; j < len; j++) {
                if (arr[min] > arr[j]) {
                    min = j;
                }
            }
            if (min != i) {
                int tmp = arr[i];
                arr[i] = arr[min];
                arr[min] = tmp;
            }
        }

    }

    public static void selectSortDemo(int[] array) {
        int len = array.length;
        int[] arr = Arrays.copyOf(array, len);

        for (int i=0;i<len;i++){
            int sub=i;
            int min=arr[i];
            for (int j=i+1;j<len;j++){
                if (min>arr[j]){
                    sub=j;
                    min=arr[j];
                }
            }
            if (sub!=i){
                int tmp=arr[i];
                arr[i]=min;
                arr[sub]=tmp;
            }
        }
    }

    public static void selectSortDemo2(int[] array) {
        int len = array.length;
        int[] arr = Arrays.copyOf(array, len);
        for (int i=0;i<len;i++){
            int min=i;
            for (int j=i;j<len;j++){
                if (arr[min]>arr[j]){
                    min=j;
                }
            }
            if (min!=i){
                int tmp=arr[i];
                arr[i]=arr[min];
                arr[min]=tmp;
            }
        }
        PrintUtils.print(arr);
    }




    /**
     *
     *
     * i=1;  j = 2
     *
     *
     */


}
